package main

import (
	"bytes"
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"io/ioutil"
	"os"
)

func main() {
	drawGradientImage()
}

func drawPureColorImage() {
	bg := image.NewNRGBA(image.Rect(0, 0, 400, 400))
	draw.Draw(bg, bg.Bounds(), &image.Uniform{color.RGBA{0, 255, 0, 255}}, image.ZP, draw.Src)
	savePngImage("pure.png", bg)
}

func drawGradientImage() {
	w := 400
	h := 400
	bg := image.NewNRGBA(image.Rect(0, 0, w, h))
	for i := 0; i < w; i++ {
		for j := 0; j < h; j++ {
			var c color.RGBA
			if i < 72 {
				c = color.RGBA{0, 0, 0, 255}
			} else if i >= 328 {
				c = color.RGBA{0, 0, 0, 0}
			} else {
				var _i uint8 = uint8(i) - 72
				c = color.RGBA{0, 0, 0, 255 - _i}
			}
			bg.Set(i, j, c)
		}
	}
	savePngImage("x.png", bg)

	dst := image.NewNRGBA(image.Rect(0, 0, w, h))
	// draw.Draw(dst, dst.Bounds(), image.NewUniform(color.RGBA{0, 255, 0, 255}), image.ZP, draw.Src)

	bts, err := ioutil.ReadFile("4.png")
	if err != nil {
		panic(err)
	}
	img1, _, err := image.Decode(bytes.NewBuffer(bts))
	if err != nil {
		panic(err)
	}
	draw.Draw(dst, dst.Bounds(), img1, img1.Bounds().Min, draw.Src)

	fileBytes, err := os.Open("/Users/mac/Downloads/20.png")
	if err != nil {
		panic(err)
	}
	defer fileBytes.Close()
	img, _, err := image.Decode(fileBytes)
	if err != nil {
		panic(err)
	}

	draw.DrawMask(dst, image.Rect(50, 50, 200, 200), img, img.Bounds().Min, bg, image.Point{100, 0}, draw.Over)

	savePngImage("gradient.png", dst)
}

func savePngImage(f string, img image.Image) {
	file, err := os.Create(f)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	err = png.Encode(file, img)
	if err != nil {
		panic(err)
	}
}
